# -*- coding: utf-8 -*-
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

from emof import core as EMOF
from emof import dumper
from emof import loader

from model import Model

class LoadClassData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        clss_id = self.request.get("id")
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        self.response.headers['Content-Type'] = 'application/xml'
        xml = "<r></r>"
        
        clss = rep.findClass(clss_id) 
        template_values = {'id': clss_id,
                           'clss': clss}
        path = os.path.join(os.getcwd(), 'xml/Class.xml')
        xml = template.render(path, template_values)

        self.response.out.write(xml)
                
class SaveClassData(webapp.RequestHandler):
    def post(self):
        m_id = self.request.get("m_id")
        clss_id = self.request.get("id")
        name = self.request.get("name")
        if self.request.get("isAbstract") == '1':
            isAbstract = True
        else:
            isAbstract = False
        superClass = list()

        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        
        
        #Lookup for superClass Object
        superClassIds = self.request.get_all("superClass")
        
        for superClassId in superClassIds:
            clss = rep.findClass(superClassId)
            superClass.append(clss)

        clss = rep.findClass(clss_id)
        clss.name = name
        clss.isAbstract = isAbstract
        clss.superClass = superClass
        
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
        
        self.response.out.write("OK")
        
class CreateClassData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        
        if self.request.get("p_id") != "":
            p_id = self.request.get("p_id")
        else:
            p_id = '-1'
            
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        fact = EMOF.Factory(rep)        
        clss = fact.create_Class()
        clss.name = 'New Class'
        clss.isAbstract = False
        
        pkg = rep.findPackage(p_id)
        if pkg:
            pkg.ownedType.append(clss)
            clss.package = pkg

        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()

        self.response.out.write(clss.id + '$$$$' + clss.name)
        
class LoadAllClass(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        id = self.request.get("id")
        
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)        
        
        classes = rep.Class

        template_values = {'classes': classes,
                           'id': id}
        
        path = os.path.join(os.getcwd(), 'xml/getSuperClass.xml')
        self.response.headers['Content-Type'] = 'application/xml'
        self.response.out.write(template.render(path, template_values))
        